library(splitstackshape)
library(PanelMatch)
library(rgdal)
library(stringdist)
library(parallel)
library(doParallel)
library(cvap)
library(wru)
library(kableExtra)
library(birdie)
library(sqldf)
library(Metrics)
library(RSQLite)
library(ebal)
library(fixest)
library(modelsummary)
options("modelsummary_format_numeric_latex" = "plain")
library(tidycensus)
library(multcomp)
library(miceadds)
library(Matching)
library(plm)
library(readstata13)
library(lubridate)
library(ggeffects)
library(scales)
library(extrafont)
library(data.table)
library(stargazer)
library(psych)
library(tidyverse)



save <- c("db", "cleanup", "theme_bc", "save", "weighted.ttest.ci", "clean_streets")


cleanup <- function(...){
  save2 <- c(save, ...)
  rm(list=ls(envir = .GlobalEnv)[! ls(envir = .GlobalEnv) %in% save2], envir = .GlobalEnv)
}

clean_streets <- function(data, vars){
  g <- data
  g$street <- apply(select(data, vars), 1,
                    function(x) paste(x[!is.na(x)], collapse = " ")
  )
  
  g <- g %>%
    select(-vars) %>%
    mutate(street = trimws(gsub("\\s+", " ", street)))
  return(g)
}

theme_bc <- function(base_size = 11, base_family = "",
                     legend.position = "right", face = "plain", ...) {
  library(extrafont)
  library(ggtext)
  half_line <- base_size/2
  theme_bw(base_family = base_family) %+replace%
    theme(plot.caption = element_textbox_simple(size = rel(0.8), hjust = 0,
                                                family = base_family,
                                                vjust = 1, margin = margin(t = half_line)),
          plot.title = element_text(size = rel(1.2), hjust = 0.5,
                                    vjust = 1, margin = margin(b = half_line),
                                    family = base_family),
          plot.subtitle = element_text(hjust = 0.5, vjust = 1, margin = margin(b = half_line)),
          legend.position = legend.position,
          text = element_text(family = base_family, face = face,
                              colour = "black", size = base_size, lineheight = 0.9,
                              hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(),
                              debug = FALSE),
          plot.caption.position =  "plot",
          ...)
}


clean_names <- function(data){
  return(gsub("[.]", "_", make.unique(make.names(tolower(colnames(data))))))
}
